import { defineComponent, onMounted, Ref, ref } from 'vue'
import { useRouter } from 'vue-router'
import moment from 'moment'
import { apiInit } from '@/services' //引入接口
import getMenuTitle from '../../AttachmentListCrumbs' //所有页面面包屑文件数据
import { columns } from '../SetBusData' //二级页面表格数据
import { cneterNewsList } from '../../type' //接口对象类型
import { client } from '@/utils'

export default function userCenter() {
	const router = useRouter()
	const api = apiInit()

	const type: any = ref(router.currentRoute.value.query.type) //获取路由中的query值
	const menuData: any = ref({}) //面包屑数据

	const positionData: any = ref([]) //表格数据
	const loading: Ref<boolean> = ref(true) //表格loading

	//获得当前页面面包屑数,tabList:所有页面面包屑文件数据
	const encodingMicrodata = async () => {
		let tabList: any = await getMenuTitle()
		const array: any = tabList.filter((item: any, index: number) => {
			return type.value === item.value
		})
		return array
	}

	const page = {
		page_index: 1,
		page_size: 10,
	}
	const query = {
		category_id: '',
		create_time: '',
		creator: '',
		end_time: '',
		title: '',
	}

	//分页
	const pageSizeOptions = ref<string[]>(['10', '20', '30', '40', '50']) //定义每页条数
	const current: any = ref() //当前页数
	const pageSizeRef: any = ref() //每页条数
	const total: any = ref() //总条数

	//处理从接口获得的列表数据 array:列表数据 pagetTtal:总条数
	const allowSamePageTransition = (
		array: any,
		pagetTtal: number | undefined
	) => {
		positionData.value = array
		positionData.value.forEach((element: any, index: any) => {
			element.index = index + 1
		})
		current.value = page.page_index //当前页数
		pageSizeRef.value = page.page_size //每页条数
		total.value = pagetTtal //总条数
	}

	//新闻列表
	const appCenterNewsList = async ({
		page = {},
		query = {},
	}: cneterNewsList) => {
		const { data, code } = await api.default.ListNewsPrivateV2POST({
			payload: { page, query },
		})
		if (code === '000000') {
			loading.value = false
			allowSamePageTransition(data?.rows, data?.page?.total)
		}
	}

	//规章制度列表
	const appRegulationsList = async ({
		page = {},
		query = {},
	}: cneterNewsList) => {
		const { data, code } = await api.default.ListRegulationPrivateV2POST({
			payload: { page, query },
		})
		if (code === '000000') {
			loading.value = false
			allowSamePageTransition(data?.rows, data?.page?.total)
		}
	}

	//公告详情列表
	const appNotificationsList = async ({
		page = {},
		query = {},
	}: cneterNewsList) => {
		const { data, code } = await api.default.ListNoticePrivateV2POST({
			payload: { page, query },
		})
		if (code === '000000') {
			loading.value = false
			allowSamePageTransition(data?.rows, data?.page?.total)
		}
	}

	//获得表格数据
	const dataDismiss = () => {
		loading.value = true
		if (type.value === 'news') {
			return appCenterNewsList({ page, query })
		} else if (type.value === 'notifications') {
			return appNotificationsList({ page, query })
		} else if (type.value === 'regulations') {
			return appRegulationsList({ page, query })
		}
	}

	//筛选框（接收子组件传递过来的值）
	const GetEnscriptKeyword = (value: any, type?: string): void => {
		if (type === 'resetPageInfo') {
			page.page_index = 1 // 当点击的是查询按钮时，每次查询重置page_index的值
		}
		query.category_id = value.ClassificationValueData?.toString()
		query.create_time =
			value.DefinitiontimeValueData && value.DefinitiontimeValueData.length
				? moment(value.DefinitiontimeValueData[0].$d).format(
						'YYYY-MM-DD HH:mm:ss'
				  )
				: ''
		query.creator = value.FounderCoordinatorValueData
		query.end_time =
			value.DefinitiontimeValueData && value.DefinitiontimeValueData.length
				? moment(value.DefinitiontimeValueData[1].$d).format(
						'YYYY-MM-DD HH:mm:ss'
				  )
				: ''
		query.title = value.InitTitleValueData
		dataDismiss()
	}

	//新闻点击统计接口
	const clickNewsCountHandle = (news_id: any) => {
		const result: any = api.default.CountHitsNewsPrivateV2POST({
			payload: { news_id },
		})
	}
	//通知公告点击数统计
	const clickNotificationsCountHandle = (notice_id: any) => {
		const result: any = api.default.CountHitsNoticePrivateV2POST({
			payload: { notice_id },
		})
	}
	//规章制度点击数统计
	const clickRegulationsCountHandle = (rules_id: any) => {
		const result: any = api.default.CountHitsRegulationPrivateV2POST({
			payload: { rules_id },
		})
	}
	//轮播图点击数统计
	const clickCarouselCountHandle = (carousel_id: any) => {
		const result: any = api.default.CountHitsCarouselPrivateV2POST({
			payload: { carousel_id },
		})
	}

	//从列表的某一条数据跳转到详情页面
	const handleClickRow = (record: any) => {
		return {
			//鼠标放上产生小手
			style: {
				cursor: 'pointer',
			},
			// 点击行
			onClick: (event: any) => {
				let id = ''
				if (type.value === 'news') {
					id = record.news_id
					clickNewsCountHandle(id)
				} else if (type.value === 'notifications') {
					id = record.notice_id
					clickNotificationsCountHandle(id)
				} else if (type.value === 'regulations') {
					id = record.rules_id
					clickRegulationsCountHandle(id)
				}
				if (record.news_type === 1) {
					client.openWindow(record.external_link)
					// window.open(params.external_link)
				} else {
					router.push({
						name: 'AttachmentDetail',
						query: { type: type.value, id: id },
					})
				}
			},
		}
	}

	//每页条请求
	const onShowSizeChange = (current: number, pageSize: number): void => {
		page.page_index = current //点击页页数
		page.page_size = pageSize //每页条数
		pageSizeRef.value = pageSize
		dataDismiss()
	}
	//页码切换请求
	const tableChange = (e: number): void => {
		page.page_index = e //点击页面
		page.page_size = pageSizeRef.value //每页条数
		dataDismiss()
	}
	onMounted(async () => {
		menuData.value = await encodingMicrodata() //执行面包屑数据获取
		dataDismiss() //执行列表数据获取
	})

	return {
		type,
		menuData,
		columns,
		positionData,
		loading,
		moment,
		pageSizeOptions,
		current,
		pageSize: pageSizeRef,
		total,
		onShowSizeChange,
		tableChange,
		GetEnscriptKeyword,
		handleClickRow,
	}
}
